package org.example;


import com.alibaba.fastjson.JSONObject;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DockerClientBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import static com.github.dockerjava.api.model.HostConfig.newHostConfig;

public class DockerExample {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 创建Docker客户端
        DockerClient dockerClient = DockerClientBuilder.getInstance("tcp://192.168.10.132:2375").build();
        //验证是否连接成功
//        Info info = dockerClient.infoCmd().exec();
//        String infoStr = JSONObject.toJSONString(info);
//        System.out.println("docker的环境信息如下：=================");
//        System.out.println(info);
        //获取所有镜像信息
//        List<Image> images = dockerClient.listImagesCmd().exec();
//        for (Image image : images) {
//            System.out.println("Image ID: " + image.getId());
//            System.out.println("Created: " + image.getCreated());
//        }
//
//        //创建容器并且启动
//        // 创建一个ExposedPort对象，指定端口号为80
//        ExposedPort tcp80  = ExposedPort.tcp(80);
//        // 创建一个Ports对象，用于绑定端口
//        Ports portBindings = new Ports();
//        // 绑定端口8088
//        portBindings.bind(tcp80, Ports.Binding.bindPort(8088));
//        // 使用dockerClient创建容器，指定容器名为nginx，端口号为8088，绑定端口80
//        CreateContainerResponse container = dockerClient.createContainerCmd("nginx:1.21")
//                .withName("nginx-text")
//                .withHostConfig(newHostConfig().withPortBindings(portBindings))
//                .withExposedPorts(tcp80).exec();
//        //启动容器
//        dockerClient.startContainerCmd(container.getId()).exec();
//        //停止容器
//        dockerClient.stopContainerCmd(container.getId()).exec();
//        //删除容器
//        dockerClient.removeContainerCmd(container.getId()).exec()
//        删除镜像
//        dockerClient.removeImageCmd(callback.getImageId()).exec();
//        关闭Docker客户端连接
//        dockerClient.close();
    }
}